//
//  UseTimeChartDataCell.swift
//  EasePods
//
//  Created by 李然 on 2024/5/30.
//

import UIKit
import EarbudsDatabase

class UseTimeChartDataCell: TableViewCell {
    
    override func makeUI() {
        super.makeUI()
        
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.addSubview(dateLabel)
        dateLabel.snp.makeConstraints { make in
            make.top.equalTo(24)
            make.leading.equalTo(20)
        }
        
        contentView.addSubview(totalUseTimeLabel)
        totalUseTimeLabel.snp.makeConstraints { make in
            make.top.equalTo(dateLabel.snp.bottom).offset(10)
            make.leading.equalTo(dateLabel)
        }
        
        contentView.addSubview(perTimeUnit)
        perTimeUnit.snp.makeConstraints { make in
            make.bottom.equalTo(totalUseTimeLabel).offset(-3)
            make.leading.equalTo(totalUseTimeLabel.snp.trailing)
        }
        
        contentView.addSubview(totalTipLabel)
        totalTipLabel.snp.makeConstraints { make in
            make.top.equalTo(totalUseTimeLabel.snp.bottom).offset(4)
            make.leading.equalTo(dateLabel)
        }
        
        contentView.addSubview(chartView)
        chartView.snp.makeConstraints { make in
            make.top.equalTo(totalTipLabel.snp.bottom).offset(40)
            make.leading.equalTo(20)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-40)
            make.height.equalTo(158)
        }
        
        contentView.addSubview(dayChartView)
        dayChartView.snp.makeConstraints { make in
            make.top.equalTo(totalTipLabel.snp.bottom).offset(40)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(-40)
            make.height.equalTo(158)
        }
    }
    
    func setData(_ data: UseTimeDetailChartData) {
        
        self.perTimeUnit.isHidden = true
        self.chartView.isHidden = true
        self.dayChartView.isHidden = true
        
        switch data.timeRangeType {
        case .daily:
            self.dateLabel.text = data.selectDate.curDayString
            self.totalUseTimeLabel.text = data.useTimeMin.hourAndMinTimeFormat
            self.dayChartView.isHidden = false
            self.dayChartView.data = data.values.first?.detail.toArray() ?? []
        case .weekly:
            self.dateLabel.text = data.selectDate.curWeekRangeString
            self.totalUseTimeLabel.text = data.useTimeMin.hourAndMinTimeFormat
            self.perTimeUnit.isHidden = false
            self.chartView.isHidden = false
            self.chartView.xValueFormatter = NovaWeekValueFormatter()
            self.chartView.minYAxisValue = 0
            self.chartView.maxYAxisValue = 1440
            setChartData(models: data.values)
        case .montly:
            self.dateLabel.text = data.selectDate.curMonthRangeString
            self.totalUseTimeLabel.text = data.useTimeMin.hourAndMinTimeFormat
            self.perTimeUnit.isHidden = false
            self.chartView.isHidden = false
            self.chartView.xValueFormatter = NovaMouthValueFormatter(date: data.selectDate)
            self.chartView.minYAxisValue = 0
            self.chartView.maxYAxisValue = 1440
            setChartData(models: data.values)
        default:
            break
        }
    }
    
    private func setChartData(models: [UsetimeModel]?) {
        guard let models = models else { return }
        var barDataSetList: [NovaSegmentBarDataSet] = []
        for model in models {
            let barDataSet = NovaSegmentBarDataSet()
            barDataSet.total = 1440
            
            let mediaTime = Double(model.totalAudioTimeMin)
            let callTime = Double(model.totalCallTimeMin)
            let standbyTime = Double(model.totalStandbyTimeMin)
            let otherTime = 1440 - mediaTime - callTime - standbyTime
            
            let mediaTimeValue = NovaSegmentBarDateValue.init(value: mediaTime, color: R.color.use_time_3_media() ?? .clear)
            let callTimeValue = NovaSegmentBarDateValue(value: callTime, color: R.color.use_time_2_phone() ?? .clear)
            let standbyTimeValue = NovaSegmentBarDateValue(value: standbyTime, color: R.color.use_time_4_unused() ?? .clear)
            let otherTimeValue = NovaSegmentBarDateValue(value: otherTime, color: R.color.use_time_5_gra_bg() ?? .clear)
            
            barDataSet.values.append(otherTimeValue)
            barDataSet.values.append(standbyTimeValue)
            barDataSet.values.append(callTimeValue)
            barDataSet.values.append(mediaTimeValue)
            
            barDataSetList.append(barDataSet)
        }
        self.chartView.data = barDataSetList
    }

    lazy var dateLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.use_time_13_item_text()
        label.font = Fonts.systemFont(ofSize: 14)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var totalUseTimeLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.use_time_14_text_main()
        label.font = Fonts.systemFont(ofSize: 28)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var perTimeUnit: UILabel = {
        let label = UILabel()
        label.textColor = R.color.use_time_14_text_main()
        label.font = Fonts.systemFont(ofSize: 12)
        label.numberOfLines = 0
        label.text = R.string.localizable.use_time_per_day()
        return label
    }()
    
    lazy var totalTipLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.use_time_15_text_second()
        label.font = Fonts.systemFont(ofSize: 12)
        label.numberOfLines = 0
        label.text = R.string.localizable.use_time_total_time()
        return label
    }()
    
    lazy var chartView: NovaSegmentBarChartView = {
        let view = NovaSegmentBarChartView()
        view.yAxisLabelCount = 5
        view.showRightAxisLabel = true
        view.yValueFormatter = NovaYAxisHourValueFormatter()
        view.placeBarColor = R.color.use_time_5_gra_bg() ?? .clear
        view.showPlaceBar = true
        view.gridLineColor = R.color.use_time_17_gra_line() ?? .clear
        view.barRatio = 2.3
        view.barStartAndEndMargin = 10
        view.barCornerRadius = 0
        return view
    }()
    
    lazy var dayChartView: UseTimeDayChartView = {
        let view = UseTimeDayChartView()
        view.xAxisLineColor = R.color.use_time_17_gra_line() ?? .gray
        view.xAxisLabelColor = R.color.use_time_16_gra_text() ?? .gray
        return view
    }()
}
